package com.shqcjd.generalplatformprojectclient.ui.activity;

import android.content.Context;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.shqcjd.generalplatformprojectclient.R;
import com.shqcjd.generalplatformprojectclient.api.ApiService;
import com.shqcjd.generalplatformprojectclient.helper.rxjavahelper.RxObserver;
import com.shqcjd.generalplatformprojectclient.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.generalplatformprojectclient.model.ResponseData;
import com.shqcjd.generalplatformprojectclient.model.pojo.GuideInfoBean;
import com.shqcjd.generalplatformprojectclient.model.pojo.eventbus.GuidanceFinishBean;
import com.shqcjd.generalplatformprojectclient.ui.base.BaseActivity;
import com.shqcjd.generalplatformprojectclient.ui.base.BasePresenter;
import com.shqcjd.generalplatformprojectclient.util.ToastUtils;
import com.shqcjd.generalplatformprojectclient.widget.SaveDraftDialog;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

public class GuideOverActivity extends BaseActivity {

    @BindView(R.id.btn_back)
    Button btnBack;
    @BindView(R.id.tv_toolbar_center)
    TextView tvToolbarCenter;
    @BindView(R.id.tv_guide_over_time)
    TextView tvGuideOverTime;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    @BindView(R.id.et_guide_over)
    EditText etGuideOver;

    private SaveDraftDialog saveDraftDialog;
    private Context context;
    private int guideId;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_guide_over;
    }

    @Override
    public void initView() {
        super.initView();
        tvToolbarCenter.setText("远程指导结束");
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showBackDialog();
            }
        });

        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = etGuideOver.getText().toString();
                if (str.length() > 0) {
                    try {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("id", guideId);
                        jsonObject.put("comment", str);
                        commitTips(jsonObject.toString());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("id", guideId);
                        jsonObject.put("comment", "本次无备忘内容");
                        commitTips(jsonObject.toString());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        getGuideInfo(guideId);
    }

    private void showBackDialog() {
        if (saveDraftDialog == null) {
            saveDraftDialog = new SaveDraftDialog(context);
            saveDraftDialog.setOnClickDialogListener(new SaveDraftDialog.setOnClickDialogListener() {
                @Override
                public void onClickSave() {
                    try {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("id", guideId);
                        jsonObject.put("comment", "本次无备忘内容");
                        commitTips(jsonObject.toString());
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onClickFinish() {
                    saveDraftDialog.dismiss();
                }
            });
        }
        saveDraftDialog.show();
        saveDraftDialog.setViewText("您确认要返回吗?", "您还未填写本次远程指导的备忘录\n填写后方" +
                "便后期查询", "确认返回", "不返回,填写备忘录");
    }


    @Override
    public void init() {
        super.init();
        context = this;
        guideId = getIntent().getIntExtra("guideId", -1);
    }

    /**
     * 填写备忘录
     */
    public void commitTips(String json) {
        ApiService.commitGuideTips(json)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        showWaitingDialog("加载中...");
                    }

                    @Override
                    public void _onNext(ResponseData data) {
                        if (data.code == 0) {
                            ToastUtils.showShort(context, "保存成功");
                            EventBus.getDefault().post(new GuidanceFinishBean());
                            finish();
                        } else {
                            ToastUtils.showShort(context, data.message);
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });
    }

    /**
     * 获取远程指导时间和费用信息
     */
    public void getGuideInfo(int id) {
        ApiService.getGuideInfo(id)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<GuideInfoBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        showWaitingDialog("加载中...");
                    }

                    @Override
                    public void _onNext(ResponseData<GuideInfoBean> draftBeanResponseData) {
                        if (draftBeanResponseData.code == 0) {
                            tvGuideOverTime.setText(draftBeanResponseData.data.totalTime);
                        } else {
                            ToastUtils.showShort(context, draftBeanResponseData.message);
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            showBackDialog();
        }
        return super.onKeyDown(keyCode, event);
    }

}
